package com.sprocomm.agingtest.views;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

/**
 * Created by resonLei on 2017/6/26.
 */

public class CDTTextView extends TextView {
    private OnCountDownTimerCallBack mOnCountDownTimerCallBack;

    private CountDownTimer mCountDownTimer ;

    private boolean streamLoadSuccess = false;

    private SoundPool mSoundPool;
    private boolean isPlayFinishTone = false;
    private int mLoopTimes = 0;


    public CDTTextView(Context context) {
        super(context);
    }

    public CDTTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CDTTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CDTTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * 设置Timer监听器
     * @param callBack
     */
    public void setOnCountDownTimerCallBack(OnCountDownTimerCallBack callBack){
        mOnCountDownTimerCallBack = callBack;
    }

    /**
     * 设置总的时间和单位时间
     * @param millisInFuture
     * @param countDownInterval
     */
    public void setCountDownTimer(final long millisInFuture, long countDownInterval){
        if (countDownInterval > 0 && millisInFuture > countDownInterval){

            mCountDownTimer = new CountDownTimer(millisInFuture,countDownInterval) {
                @Override
                public void onTick(long millisUntilFinished) {
                    if (mOnCountDownTimerCallBack != null){
                        mOnCountDownTimerCallBack.setOnTick(millisUntilFinished);
                    }
                    setText(longTime2String(millisUntilFinished));
                }

                @Override
                public void onFinish() {
                    if (mOnCountDownTimerCallBack != null){
                        mOnCountDownTimerCallBack.setOnFinish();
                    }
                    setText("PASS");
                    //结束之后播放音乐
                    if (streamLoadSuccess){
                        mSoundPool.play(1,1,1,1, mLoopTimes,1);
                    }else{
                        Log.e("CDTTextView","soundpool load not complete When time is running out");
                    }
                }
            };
        }else {
            throw new IllegalStateException("时间不能小于0，且前者应该大于后者");
        }
    }

    /**
     * 监听接口
     */
    public interface OnCountDownTimerCallBack {
        void setOnTick(long millisUntilFinished);
        void setOnFinish();
    }

    /**
     * timer 开始
     */
    public void setTimerStart(){
        if (mCountDownTimer == null){
            throw new IllegalStateException("should set a timer");
        }

        mCountDownTimer.start();
    }
    /**
     * timer 结束
     */
    public void setTimerCancle(){
        if (mCountDownTimer == null){
            throw new IllegalStateException("should set a timer");
        }
        mCountDownTimer.cancel();
    }

    /**
     * 把long时间转换成易识别时间string
     * @param time
     * @return
     */
    private String longTime2String(long time){
        long second = time/1000;
        int h = Integer.parseInt(String.valueOf(second/3600));
        int m = Integer.parseInt(String.valueOf((second%3600)/60));
        int s = Integer.parseInt(String.valueOf(second%60));
        return h+":"+m+":"+s;
    }

    /**
     * 在及时完成之后播放提示音乐
     */
    private void setSoundPool(Context context,int musicRes){
        SoundPool.Builder builder = new SoundPool.Builder();
        builder.setMaxStreams(1);
        AudioAttributes.Builder builder1 = new AudioAttributes.Builder();
        builder1.setLegacyStreamType(AudioManager.STREAM_SYSTEM);
        //加载一个AudioAttributes
        builder.setAudioAttributes(builder1.build());
        mSoundPool = builder.build();
        mSoundPool.load(context, musicRes,1);
        mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                streamLoadSuccess = true;
            }
        });
    }

    /**
     * 结束铃声的播放
     */
    public void sotpFinishTone(){
        if (mSoundPool != null){
            mSoundPool.release();
        }
    }

    /**
     * 设置一个结束铃声资源
     * @param loopTimes 循环次数
     * @param context
     * @param musicRes 铃声资源
     * @param isPlayFinishTone 是否开启
     */
    public void setFinishTone(Context context,int musicRes,int loopTimes,boolean isPlayFinishTone){
        if (musicRes == 0){
            throw new IllegalArgumentException("musicRes not be null");
        }
        if (context == null){
            throw new IllegalArgumentException("context is null");
        }
        if (loopTimes <= -2){
            throw new IllegalArgumentException("loopTimes should be greater than -1");
        }
        this.isPlayFinishTone = isPlayFinishTone;
        mLoopTimes = loopTimes;
        if (this.isPlayFinishTone){
            setSoundPool(context,musicRes);
        }
    }

}
